{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4_9V05MNAGMV"
   },
   "source": [
    "## Swin transformer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "XyE9r-S2mg1X",
    "outputId": "aa591258-7f89-4dfb-a846-b31d9d590f84"
   },
   "outputs": [],
   "source": [
    "# from google.colab import drive\n",
    "# drive.mount('/content/drive')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "p4k4azzGr4wK"
   },
   "outputs": [],
   "source": [
    "#cd /content/drive/MyDrive/sunandini"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "Ite3R2N8rdoN"
   },
   "outputs": [],
   "source": [
    "#!unzip \"/content/drive/MyDrive/sunandini/trans_data.zip\" "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Installing required packages\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "c6HQsaKvAR8v",
    "outputId": "666b08c2-dc33-42cd-8c81-70a64e4d2e3e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: timm in c:\\users\\rajes\\anaconda3\\lib\\site-packages (0.6.13)\n",
      "Requirement already satisfied: pyyaml in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from timm) (6.0)\n",
      "Requirement already satisfied: huggingface-hub in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from timm) (0.13.3)\n",
      "Requirement already satisfied: torch>=1.7 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from timm) (2.0.0)\n",
      "Requirement already satisfied: torchvision in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from timm) (0.15.0)\n",
      "Requirement already satisfied: filelock in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from torch>=1.7->timm) (3.9.0)\n",
      "Requirement already satisfied: typing-extensions in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from torch>=1.7->timm) (3.7.4.3)\n",
      "Requirement already satisfied: sympy in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from torch>=1.7->timm) (1.11.1)\n",
      "Requirement already satisfied: networkx in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from torch>=1.7->timm) (2.8.4)\n",
      "Requirement already satisfied: jinja2 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from torch>=1.7->timm) (2.11.3)\n",
      "Requirement already satisfied: tqdm>=4.42.1 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from huggingface-hub->timm) (4.65.0)\n",
      "Requirement already satisfied: packaging>=20.9 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from huggingface-hub->timm) (23.0)\n",
      "Requirement already satisfied: requests in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from huggingface-hub->timm) (2.27.1)\n",
      "Requirement already satisfied: numpy in c:\\users\\rajes\\appdata\\roaming\\python\\python39\\site-packages (from torchvision->timm) (1.19.5)\n",
      "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from torchvision->timm) (9.4.0)\n",
      "Requirement already satisfied: colorama in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from tqdm>=4.42.1->huggingface-hub->timm) (0.4.6)\n",
      "Requirement already satisfied: MarkupSafe>=0.23 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from jinja2->torch>=1.7->timm) (2.0.1)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from requests->huggingface-hub->timm) (1.26.9)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from requests->huggingface-hub->timm) (2022.12.7)\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from requests->huggingface-hub->timm) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from requests->huggingface-hub->timm) (3.4)\n",
      "Requirement already satisfied: mpmath>=0.19 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from sympy->torch>=1.7->timm) (1.2.1)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Error parsing requirements for tensorflow-estimator: [Errno 2] No such file or directory: 'c:\\\\users\\\\rajes\\\\anaconda3\\\\lib\\\\site-packages\\\\tensorflow_estimator-2.10.0.dist-info\\\\METADATA'\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: einops in c:\\users\\rajes\\anaconda3\\lib\\site-packages (0.6.0)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Error parsing requirements for tensorflow-estimator: [Errno 2] No such file or directory: 'c:\\\\users\\\\rajes\\\\anaconda3\\\\lib\\\\site-packages\\\\tensorflow_estimator-2.10.0.dist-info\\\\METADATA'\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: tensorboardX in c:\\users\\rajes\\anaconda3\\lib\\site-packages (2.6)\n",
      "Requirement already satisfied: protobuf<4,>=3.8.0 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from tensorboardX) (3.20.3)\n",
      "Requirement already satisfied: numpy in c:\\users\\rajes\\appdata\\roaming\\python\\python39\\site-packages (from tensorboardX) (1.19.5)\n",
      "Requirement already satisfied: packaging in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from tensorboardX) (23.0)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Error parsing requirements for tensorflow-estimator: [Errno 2] No such file or directory: 'c:\\\\users\\\\rajes\\\\anaconda3\\\\lib\\\\site-packages\\\\tensorflow_estimator-2.10.0.dist-info\\\\METADATA'\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: medpy in c:\\users\\rajes\\anaconda3\\lib\\site-packages (0.4.0)\n",
      "Requirement already satisfied: scipy>=1.1.0 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from medpy) (1.7.3)\n",
      "Requirement already satisfied: SimpleITK>=1.1.0 in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from medpy) (2.2.1)\n",
      "Requirement already satisfied: numpy>=1.11.0 in c:\\users\\rajes\\appdata\\roaming\\python\\python39\\site-packages (from medpy) (1.19.5)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Error parsing requirements for tensorflow-estimator: [Errno 2] No such file or directory: 'c:\\\\users\\\\rajes\\\\anaconda3\\\\lib\\\\site-packages\\\\tensorflow_estimator-2.10.0.dist-info\\\\METADATA'\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: yacs in c:\\users\\rajes\\anaconda3\\lib\\site-packages (0.1.8)\n",
      "Requirement already satisfied: PyYAML in c:\\users\\rajes\\anaconda3\\lib\\site-packages (from yacs) (6.0)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Error parsing requirements for tensorflow-estimator: [Errno 2] No such file or directory: 'c:\\\\users\\\\rajes\\\\anaconda3\\\\lib\\\\site-packages\\\\tensorflow_estimator-2.10.0.dist-info\\\\METADATA'\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: torchinfo in c:\\users\\rajes\\anaconda3\\lib\\site-packages (1.7.2)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Error parsing requirements for tensorflow-estimator: [Errno 2] No such file or directory: 'c:\\\\users\\\\rajes\\\\anaconda3\\\\lib\\\\site-packages\\\\tensorflow_estimator-2.10.0.dist-info\\\\METADATA'\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: einops in c:\\users\\rajes\\anaconda3\\lib\\site-packages (0.6.0)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Error parsing requirements for tensorflow-estimator: [Errno 2] No such file or directory: 'c:\\\\users\\\\rajes\\\\anaconda3\\\\lib\\\\site-packages\\\\tensorflow_estimator-2.10.0.dist-info\\\\METADATA'\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -rotobuf (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ensorflow-intel (c:\\users\\rajes\\anaconda3\\lib\\site-packages)\n"
     ]
    }
   ],
   "source": [
    "!pip install timm\n",
    "!pip install einops\n",
    "!pip install tensorboardX\n",
    "!pip install medpy\n",
    "!pip install yacs\n",
    "!pip install torchinfo\n",
    "!pip install einops"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# importing all required packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "7KyLF8JS9ulr"
   },
   "outputs": [],
   "source": [
    "# importing all required packages\n",
    "import math\n",
    "import copy\n",
    "import torch.optim as optim\n",
    "from torch.nn.parallel import DataParallel\n",
    "from collections import OrderedDict\n",
    "from torch import nn\n",
    "from torchinfo import summary\n",
    "import copy\n",
    "import torch \n",
    "from torch import nn\n",
    "from torch.nn import init\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "from sklearn.metrics import jaccard_score, precision_score, recall_score\n",
    "import numpy as np\n",
    "from torch import nn\n",
    "import torch.utils.checkpoint as checkpoint\n",
    "\n",
    "from timm.models.layers import DropPath, to_2tuple, trunc_normal_\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "oYc_OJlak0zW"
   },
   "outputs": [],
   "source": [
    "from model import *"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# calling the SwinDeeplab model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "bMLJrGiw9uW0",
    "outputId": "fb374ec8-75c9-4bae-86e2-601edb264f6d"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\rajes\\anaconda3\\lib\\site-packages\\torch\\functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at C:\\cb\\pytorch_1000000000000\\work\\aten\\src\\ATen\\native\\TensorShape.cpp:3484.)\n",
      "  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]\n"
     ]
    }
   ],
   "source": [
    "model = SwinDeepLab(\n",
    "    EncoderConfig, \n",
    "    ASPPConfig, \n",
    "    DecoderConfig\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Printing summary of the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "NVDIAd5vk0zc",
    "outputId": "fe3fe2a6-bc07-434e-c306-4199185cd4d5"
   },
   "outputs": [],
   "source": [
    "# summary(model=model,input_size=(25, 3, 224, 224), # (batch_size, color_channels, height, width)\n",
    "#         # col_names=[\"input_size\"], # uncomment for smaller output\n",
    "#         col_names=[\"input_size\", \"output_size\", \"num_params\", \"trainable\"],\n",
    "#         col_width=20,\n",
    "#         row_settings=[\"var_names\"])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# defining the conditions for pretrained weights \n",
    "## in this code we are not using pretrained weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "FgXH7HsgnGFW",
    "outputId": "5e723cd3-57be-4e8b-b0df-d0ef9e83a1bf"
   },
   "outputs": [],
   "source": [
    "# if EncoderConfig.encoder_name == 'swin' and EncoderConfig.load_pretrained:\n",
    "#     model.encoder.load_from('/content/drive/MyDrive/sunandini/swin_tiny_patch4_window7_224 (1).pth')\n",
    "# if ASPPConfig.aspp_name == 'swin' and ASPPConfig.load_pretrained:\n",
    "#     model.aspp.load_from('/content/drive/MyDrive/sunandini/swin_tiny_patch4_window7_224 (1).pth')\n",
    "# if DecoderConfig.decoder_name == 'swin' and DecoderConfig.load_pretrained and not DecoderConfig.extended_load:\n",
    "#     model.decoder.load_from('/content/drive/MyDrive/sunandini/swin_tiny_patch4_window7_224 (1).pth')\n",
    "# if DecoderConfig.decoder_name == 'swin' and DecoderConfig.load_pretrained and DecoderConfig.extended_load:\n",
    "#     model.decoder.load_from_extended('/content/drive/MyDrive/sunandini/swin_tiny_patch4_window7_224 (1).pth')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### checking the device : cuda or cpu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "BHRskVAqm7cm",
    "outputId": "a01f4f51-c3cb-4bf7-fb34-c2aeaccb7034"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "device: cuda\n"
     ]
    }
   ],
   "source": [
    "device = torch.device(\"cuda\") if torch.cuda.is_available() else torch.device(\"cpu\")\n",
    "print(\"device:\", device)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# defining the trainloader and validation data loader functions by applying transformations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "id": "BzdN8MB9PBxo"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from PIL import Image\n",
    "import torch\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "import torchvision.transforms as transforms\n",
    "import torchvision.transforms.functional as F\n",
    "\n",
    "class ImageMaskDataset(Dataset):\n",
    "    def __init__(self, image_folder, mask_folder, transform=None):\n",
    "        self.image_files = os.listdir(image_folder)\n",
    "        self.mask_files = os.listdir(mask_folder)\n",
    "        self.image_folder = image_folder\n",
    "        self.mask_folder = mask_folder\n",
    "        self.transform = transform\n",
    "        \n",
    "    def __len__(self):\n",
    "        return len(self.image_files)\n",
    "    \n",
    "    def __getitem__(self, idx):\n",
    "        # Open the corresponding image and mask files\n",
    "        img = Image.open(os.path.join(self.image_folder, self.image_files[idx])).convert(\"RGB\")\n",
    "        mask = Image.open(os.path.join(self.mask_folder, self.mask_files[idx])).convert(\"L\")\n",
    "        #print(img.mode)\n",
    "        # Apply the transformations to the input image and segmentation mask\n",
    "        if self.transform:\n",
    "            img = self.transform(img)\n",
    "            #mask = F.resize(mask, (224, 224))\n",
    "            mask = self.transform(mask)\n",
    "\n",
    "        # Normalize the input image\n",
    "        img = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(img)\n",
    "        #mask  = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(mask)\n",
    "        \n",
    "        return img, mask\n",
    "\n",
    "# Define the transformations to be applied\n",
    "transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor()])\n",
    "#transform.transforms[-1].requires_grad = True\n",
    "# Set the paths to the image and mask folders\n",
    "\n",
    "train_image_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/data/train/images/\"\n",
    "train_mask_folder =\"C:/Users/rajes/OneDrive/Desktop/dataset/data/train/masks/\"\n",
    "val_image_folder =  \"C:/Users/rajes/OneDrive/Desktop/dataset/data/val/images/\"\n",
    "val_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/data/val/masks/\"\n",
    "# Create the dataset\n",
    "train_dataset = ImageMaskDataset(train_image_folder, train_mask_folder, transform=transform)\n",
    "val_dataset = ImageMaskDataset(val_image_folder, val_mask_folder, transform=transform)\n",
    "# Create the DataLoader\n",
    "batch_size = 4\n",
    "train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)\n",
    "val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=True)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Training the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "EnCN0idSk0ze"
   },
   "outputs": [],
   "source": [
    "model = model.to(device)\n",
    "torch.manual_seed(0)\n",
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
    "\n",
    "# Define the binary cross-entropy loss function\n",
    "criterion = nn.BCEWithLogitsLoss()\n",
    "optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=0.001,weight_decay=0.001)\n",
    "#optimizer = torch.optim.Adam(model.parameters(), lr=0.001,weight_decay=0.01)\n",
    "\n",
    "train_losses = []\n",
    "train_accs = []\n",
    "val_losses = []\n",
    "val_accs = []\n",
    "\n",
    "# Set the number of epochs\n",
    "n_epochs = 200\n",
    "\n",
    "# Check if there are multiple GPUs available\n",
    "if torch.cuda.device_count() > 1:\n",
    "    print(\"Using\", torch.cuda.device_count(), \"GPUs\")\n",
    "    model = nn.DataParallel(model)\n",
    "else:\n",
    "    print(\"Using single GPU\")\n",
    "\n",
    "best_val_loss = float('inf')\n",
    "min_epochs=10\n",
    "for epoch in range(n_epochs):\n",
    "    running_loss = 0.0\n",
    "    correct_train = 0\n",
    "    total_train = 0\n",
    "    model.train()\n",
    "    for i, data in enumerate(train_loader, 0):\n",
    "        inputs, labels = data\n",
    "        inputs, labels = inputs.to(device), labels.to(device)\n",
    "        optimizer.zero_grad()\n",
    "        outputs = model(inputs)\n",
    "        loss = criterion(outputs, labels)\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        running_loss += loss.item()\n",
    "        # Compute total number of training examples processed in the current epoch\n",
    "        total_train += labels.numel()\n",
    "        # Compute number of correctly predicted training examples in the current batch\n",
    "        predicted = (torch.sigmoid(outputs) > 0.5).float()\n",
    "        correct_train += (predicted == labels).sum().item()\n",
    "        # if i % 10 == 9:  # Print every 10 mini-batches\n",
    "        #     print('[%d, %5d] loss: %.3f' %(epoch + 1, i + 1, running_loss / 10))\n",
    "    # Calculate total training loss over all batches in the current epoch\n",
    "    train_loss = running_loss / len(train_loader.dataset)\n",
    "    # Calculate training accuracy over all batches in the current epoch\n",
    "    train_acc = correct_train / total_train\n",
    "    print('Epoch %d | Training Loss: %.4f | Training Accuracy: %.2f %%' % (epoch + 1, train_loss, train_acc))\n",
    "# append current epoch's accuracy and loss values to the respective lists\n",
    "    train_losses.append(train_loss)\n",
    "    train_accs.append(train_acc)\n",
    "\n",
    "    \n",
    "    # Evaluate the model on the validation set\n",
    "    model.eval()\n",
    "    correct_val = 0\n",
    "    val_loss = 0.0\n",
    "    total_val = 0\n",
    "    with torch.no_grad():\n",
    "        for data in val_loader:\n",
    "            inputs, labels = data\n",
    "            inputs, labels = inputs.to(device), labels.to(device)\n",
    "            outputs = model(inputs)\n",
    "            loss = criterion(outputs, labels)\n",
    "            outputs = torch.sigmoid(outputs)\n",
    "            predicted = (outputs > 0.5).float()\n",
    "            #print(\"predicted\",predicted)\n",
    "            \n",
    "            val_loss += loss.item()\n",
    "            correct_val += (predicted == labels).sum().item()\n",
    "            total_val += labels.numel()\n",
    "        val_loss /= len(val_loader)\n",
    "        # Save the model weights if the validation loss is smaller than the current best validation loss\n",
    "        if val_loss < best_val_loss:\n",
    "            best_val_loss = val_loss\n",
    "            torch.save({'model_state_dict': model.state_dict()}, 'best_model_weights.pth')\n",
    "        val_acc = correct_val / total_val\n",
    "        val_losses.append(val_loss)\n",
    "        val_accs.append(val_acc)\n",
    "            # check if early stopping criterion is met\n",
    "        # if epoch > min_epochs and val_loss > best_val_loss:\n",
    "        #     print('Early stopping!')\n",
    "        #     break\n",
    "    print('Epoch [{}/{}], Validation Loss: {:.4f}, Validation Accuracy: {:.2f}%'.format(epoch+1, n_epochs, val_loss, val_acc))\n",
    "    # Save the model weights every 10 epochs\n",
    "    if (epoch + 1) % 10 == 0:\n",
    "        torch.save({'model_state_dict': model.state_dict()}, 'model_weights_epoch{}.pth'.format(epoch+1))\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting the loss and accuracy curves"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "p_fzv62_bG5d"
   },
   "outputs": [],
   "source": [
    "# plot accuracy and loss curves for all epochs so far\n",
    "fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))\n",
    "\n",
    "# plot training and validation loss curves\n",
    "axes[0].plot(train_losses, label='Training Loss')\n",
    "axes[0].plot(val_losses, label='Validation Loss')\n",
    "axes[0].set_xlabel('Epoch')\n",
    "axes[0].set_title('Training and Validation Loss')\n",
    "axes[0].legend()\n",
    "\n",
    "\n",
    "# plot training and validation accuracy curves\n",
    "axes[1].plot(train_accs, label='Training Accuracy')\n",
    "axes[1].plot(val_accs, label='Validation Accuracy')\n",
    "axes[1].set_xlabel('Epoch')\n",
    "axes[1].set_ylabel('Accuracy')\n",
    "axes[1].set_title('Training and Validation Accuracy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 294,
   "metadata": {
    "id": "e9SzscD-sd7E"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "machine_shape": "hm",
   "provenance": []
  },
  "gpuClass": "premium",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
